package com.pfsbuilder.Dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTree;

import com.pfsbuilder.CommonLib;
import com.pfsbuilder.PExceptionDialog;
import com.pfsbuilder.PFSOwnerPanel;
import com.pfsbuilder.PFSPermissionPanel;
import com.pfsbuilder.PFSTreeModel;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * <p>
 * Title: PFS
 * </p>
 *
 * <p>
 * Description:
 * </p>
 *
 * <p>
 * Copyright: Copyright (c) 2005
 * </p>
 *
 * <p>
 * Company: Petersoft
 * </p>
 *
 * @author Peter
 * @version 2005
 */
public class AutoGenerateDialog extends JDialog implements Runnable {
	JPanel pTypePanel = new JPanel();
	JPanel pNamePanel = new JPanel();
	JPanel pControlPanel = new JPanel();
	JButton pOKButton = new JButton();
	JButton pCancelButton = new JButton();
	JLabel pTypeLabel = new JLabel();
	BorderLayout borderLayout1 = new BorderLayout();
	JRadioButton pFileRadioButton = new JRadioButton();
	JPanel JPanel3 = new JPanel();
	JRadioButton pDirectoryRadioButton = new JRadioButton();
	FlowLayout flowLayout1 = new FlowLayout(FlowLayout.LEFT);
	JLabel pNameLabel = new JLabel();
	ButtonGroup buttonGroup1 = new ButtonGroup();
	BorderLayout borderLayout2 = new BorderLayout();
	JPanel JPanel4 = new JPanel();
	JLabel pFromLabel = new JLabel();
	JTextField pFromTextField = new JTextField();
	JLabel pToLabel = new JLabel();
	JTextField pToTextField = new JTextField();
	JLabel pSteJLabel = new JLabel();
	JTextField pSteJTextField = new JTextField();
	JLabel pPrefixLabel = new JLabel();
	JTextField pPrefixTextField = new JTextField();
	FlowLayout flowLayout2 = new FlowLayout(FlowLayout.LEFT);
	JPanel pFilesizePanel = new JPanel();
	BorderLayout borderLayout3 = new BorderLayout();
	JLabel pFilesizeLabel = new JLabel();
	JPanel JPanel6 = new JPanel();
	JPanel pFixedSizePanel = new JPanel();
	JRadioButton pFixedSizeRadioButton = new JRadioButton();
	FlowLayout flowLayout3 = new FlowLayout(FlowLayout.LEFT);
	JTextField pFilesizeTextField = new JTextField();
	JRadioButton pByteRadioButton = new JRadioButton();
	JRadioButton pMBRadioButton = new JRadioButton();
	JRadioButton pKBRadioButton = new JRadioButton();
	ButtonGroup buttonGroup2 = new ButtonGroup();
	JPanel JPanel7 = new JPanel();
	JRadioButton pSameAsFilenameRadioButton = new JRadioButton();
	FlowLayout flowLayout4 = new FlowLayout(FlowLayout.LEFT);
	ButtonGroup buttonGroup3 = new ButtonGroup();
	PFSPermissionPanel pFSPermissionPanel = new PFSPermissionPanel();
	PFSOwnerPanel pFSOwnerPanel = new PFSOwnerPanel();
	JTree jDirectoryTree;
	String projectFile;
	JPanel pMainPanel = new JPanel();
	BorderLayout borderLayout4 = new BorderLayout();
	JPanel pFileContentPanel = new JPanel();
	JLabel JLabel1 = new JLabel();
	JPanel JPanel2 = new JPanel();
	BorderLayout borderLayout5 = new BorderLayout();
	JPanel JPanel1 = new JPanel();
	JPanel JPanel5 = new JPanel();
	JRadioButton JRadioButton1 = new JRadioButton();
	JRadioButton JRadioButton2 = new JRadioButton();
	FlowLayout flowLayout5 = new FlowLayout(FlowLayout.LEFT);
	FlowLayout flowLayout6 = new FlowLayout(FlowLayout.LEFT);
	ButtonGroup buttonGroup4 = new ButtonGroup();
	JTextField pFileContentTextField = new JTextField();
	JTextField pFileContentFromTextField = new JTextField();
	JLabel JLabel2 = new JLabel();
	JTextField pFileContentToTextField = new JTextField();
	JPanel pProgressPanel = new JPanel();
	BorderLayout borderLayout6 = new BorderLayout();
	JScrollPane pProgressScrollPane = new JScrollPane();
	JTable pProgressTable = new JTable();
	WriteDialog_TableModel pWriteDialog_TableModel = new WriteDialog_TableModel();
	boolean stopThread;
	JLabel pWarningLabel = new JLabel();

	public AutoGenerateDialog(JFrame jMainFrame, JTree jDirectoryTree, String projectFile) throws HeadlessException {
		super(jMainFrame, true);
		this.jDirectoryTree = jDirectoryTree;
		this.projectFile = projectFile;
		try {
			pFSOwnerPanel = new PFSOwnerPanel(this, new File(projectFile), null);
			pFSPermissionPanel = new PFSPermissionPanel(new File(projectFile), null);
			jbInit();
		} catch (Exception ex) {
			ex.printStackTrace();
			if (com.pfsbuilder.Global.GUI) {
				new PExceptionDialog(ex, com.pfsbuilder.Global.sessionNumber).setVisible(true);
			}
		}
	}

	private void jbInit() throws Exception {
		getContentPane().setLayout(borderLayout4);
		pCancelButton.addActionListener(new PAutoGenerateDialog_pCancelButton_actionAdapter(this));
		JLabel1.setPreferredSize(new Dimension(120, 18));
		JLabel1.setText("File content : ");
		pFileContentPanel.setLayout(borderLayout5);
		JPanel2.setInputVerifier(null);
		JRadioButton1.setSelected(true);
		JRadioButton1.setText("from");
		JRadioButton2.setText("");
		JPanel5.setLayout(flowLayout5);
		JPanel1.setLayout(flowLayout6);
		pFileContentTextField.setPreferredSize(new Dimension(30, 20));
		pFileContentTextField.setText("0");
		pPrefixTextField.setPreferredSize(new Dimension(50, 21));
		pFileContentFromTextField.setPreferredSize(new Dimension(50, 21));
		pFileContentFromTextField.setText("0");
		JLabel2.setText("to");
		pFileContentToTextField.setPreferredSize(new Dimension(50, 21));
		pFileContentToTextField.setText("9");
		pProgressPanel.setLayout(borderLayout6);
		pProgressTable.setModel(pWriteDialog_TableModel);
		pWarningLabel.setForeground(Color.red);
		buttonGroup2.add(pByteRadioButton);
		buttonGroup2.add(pKBRadioButton);
		buttonGroup2.add(pMBRadioButton);
		buttonGroup2.add(pSameAsFilenameRadioButton);
		buttonGroup3.add(pSameAsFilenameRadioButton);
		buttonGroup3.add(pFixedSizeRadioButton);
		pOKButton.setText("OK");
		pOKButton.addActionListener(new PAutoGenerateDialog_pOKButton_actionAdapter(this));
		pCancelButton.setText("Cancel");
		pTypeLabel.setPreferredSize(new Dimension(120, 18));
		pTypeLabel.setText("Type : ");
		pTypePanel.setLayout(borderLayout1);
		pFileRadioButton.setSelected(true);
		pFileRadioButton.setText("file");
		pDirectoryRadioButton.setText("directory");
		JPanel3.setLayout(flowLayout1);
		pNameLabel.setPreferredSize(new Dimension(120, 18));
		pNameLabel.setText("Name : ");
		pNamePanel.setLayout(borderLayout2);
		pFromLabel.setText("from");
		pToLabel.setText("to");
		pToTextField.setPreferredSize(new Dimension(50, 21));
		pToTextField.setText("10");
		pSteJLabel.setText("step");
		pSteJTextField.setPreferredSize(new Dimension(50, 21));
		pSteJTextField.setText("1");
		pFromTextField.setPreferredSize(new Dimension(50, 21));
		pFromTextField.setText("0");
		pPrefixLabel.setText("prefix");
		JPanel4.setLayout(flowLayout2);
		pPrefixTextField.setText("file");
		pFilesizePanel.setLayout(borderLayout3);
		pFilesizeLabel.setPreferredSize(new Dimension(120, 18));
		pFilesizeLabel.setText("File size : ");
		pFixedSizeRadioButton.setText("fixed size");
		pFixedSizePanel.setLayout(flowLayout3);
		pFilesizeTextField.setPreferredSize(new Dimension(60, 21));
		pFilesizeTextField.setText("1");
		pByteRadioButton.setSelected(true);
		pByteRadioButton.setText("byte");
		pMBRadioButton.setText("MB");
		pKBRadioButton.setText("KB");
		pSameAsFilenameRadioButton.setText("base on file name");
		JPanel7.setLayout(flowLayout4);
		this.getContentPane().setBackground(Color.white);
		BoxLayout pMainPanelLayout = new BoxLayout(pMainPanel, javax.swing.BoxLayout.Y_AXIS);
		pMainPanel.setLayout(pMainPanelLayout);
		pMainPanel.add(pTypePanel);
		pMainPanel.add(pNamePanel);
		pMainPanel.add(pFileContentPanel);
		pMainPanel.add(pFilesizePanel);
		pMainPanel.add(pFSPermissionPanel);
		pMainPanel.add(pFSOwnerPanel);
		pControlPanel.add(pWarningLabel);
		pControlPanel.add(pOKButton);
		pControlPanel.add(pCancelButton);
		JPanel3.add(pFileRadioButton);
		JPanel3.add(pDirectoryRadioButton);
		pTypePanel.add(pTypeLabel, java.awt.BorderLayout.WEST);
		pTypePanel.add(JPanel3, java.awt.BorderLayout.CENTER);

		buttonGroup1.add(pFileRadioButton);
		buttonGroup1.add(pDirectoryRadioButton);
		pNamePanel.add(pNameLabel, java.awt.BorderLayout.WEST);
		pNamePanel.add(JPanel4, java.awt.BorderLayout.CENTER);
		JPanel4.add(pPrefixLabel);
		JPanel4.add(pPrefixTextField);
		JPanel4.add(pFromLabel);
		JPanel4.add(pFromTextField);
		JPanel4.add(pToLabel);
		JPanel4.add(pToTextField);
		JPanel4.add(pSteJLabel);
		JPanel4.add(pSteJTextField);
		pFilesizePanel.add(pFilesizeLabel, java.awt.BorderLayout.WEST);
		BoxLayout JPanel6Layout = new BoxLayout(JPanel6, javax.swing.BoxLayout.Y_AXIS);
		JPanel6.setLayout(JPanel6Layout);
		pFilesizePanel.add(JPanel6, java.awt.BorderLayout.CENTER);
		pFilesizePanel.add(pFilesizeLabel, java.awt.BorderLayout.WEST);
		JPanel6.add(JPanel7);
		JPanel7.add(pSameAsFilenameRadioButton);
		JPanel6.add(pFixedSizePanel);
		pFixedSizePanel.add(pFixedSizeRadioButton);
		pFixedSizePanel.add(pFilesizeTextField);
		pFixedSizePanel.add(pByteRadioButton);
		pFixedSizePanel.add(pKBRadioButton);
		pFixedSizePanel.add(pMBRadioButton);
		pSameAsFilenameRadioButton.setSelected(true);
		getContentPane().add(pControlPanel, BorderLayout.SOUTH);
		getContentPane().add(pMainPanel, BorderLayout.CENTER);
		BoxLayout JPanel2Layout = new BoxLayout(JPanel2, javax.swing.BoxLayout.Y_AXIS);
		JPanel2.setLayout(JPanel2Layout);
		pFileContentPanel.add(JPanel2, java.awt.BorderLayout.CENTER);
		JPanel2.add(JPanel5);
		JPanel5.add(JRadioButton1);
		JPanel5.add(pFileContentFromTextField);
		JPanel5.add(JLabel2);
		JPanel5.add(pFileContentToTextField);
		JPanel2.add(JPanel1);
		JPanel1.add(JRadioButton2);
		JPanel1.add(pFileContentTextField);
		pFileContentPanel.add(JLabel1, java.awt.BorderLayout.WEST);
		setSize(600, 450);
		CommonLib.centerDialog(this);
		buttonGroup4.add(JRadioButton1);
		buttonGroup4.add(JRadioButton2);
		pProgressPanel.add(pProgressScrollPane, java.awt.BorderLayout.CENTER);
		pProgressScrollPane.getViewport().add(pProgressTable);
		this.setTitle("Auto Generate Files");
	}

	public void pOKButton_actionPerformed(ActionEvent e) {
		if (pOKButton.getText().equals("OK")) {
			this.getContentPane().remove(pMainPanel);
			getContentPane().add(pProgressPanel, BorderLayout.EAST);
			this.getContentPane().validate();
			pOKButton.setText("Stop");

			stopThread = false;
			new Thread(this).start();
		} else if (pOKButton.getText().equals("Resume")) {
			pOKButton.setText("Stop");
			stopThread = false;
		} else {
			stopThread = true;
			pOKButton.setText("Resume");
		}

	}

	public void run() {
		pWarningLabel.setText("");
		File currentDirectory = (File) jDirectoryTree.getLastSelectedPathComponent();
		try {
			int from = Integer.parseInt(pFromTextField.getText());
			int to = Integer.parseInt(pToTextField.getText());
			int step = Integer.parseInt(pSteJTextField.getText());
			String prefix = pPrefixTextField.getText();

			if (pFileRadioButton.isSelected()) {
				for (int x = from; x <= to; x += step) {
					try {
						while (stopThread) {
							Thread.currentThread().sleep(1000);
						}
						if (this.isVisible() == false) {
							return;
						}
						String filename = currentDirectory + File.separator + prefix + x;
						pWriteDialog_TableModel.add(filename, "");
						pProgressScrollPane.getViewport().setViewPosition(new Point(0, pProgressTable.getMaximumSize().height));

						new File(filename).createNewFile();
						if (pSameAsFilenameRadioButton.isSelected()) {
							writeFileContent(filename, x);
						} else {
							long size = Long.parseLong(pFilesizeTextField.getText());
							if (pKBRadioButton.isSelected()) {
								size *= 1024;
							}
							if (pMBRadioButton.isSelected()) {
								size *= 1024 * 1024;
							}

							writeFileContent(filename, size);
						}

						pWriteDialog_TableModel.setValueAt("done", pWriteDialog_TableModel.getRowCount() - 1, 1);

						pFSPermissionPanel.writeIni(filename);
						pFSOwnerPanel.writeIni(filename);
					} catch (Exception ex) {
						pWriteDialog_TableModel.setValueAt("fail", pWriteDialog_TableModel.getRowCount() - 1, 1);
					}
				}
			} else {
				for (int x = from; x <= to; x += step) {
					while (stopThread) {
						Thread.currentThread().sleep(1000);
					}
					if (this.isVisible() == false) {
						return;
					}

					String filename = currentDirectory + File.separator + prefix + x;
					pWriteDialog_TableModel.add(filename, "");
					pProgressScrollPane.getViewport().setViewPosition(new Point(0, pProgressTable.getMaximumSize().height));

					new File(filename).mkdir();
					pWriteDialog_TableModel.setValueAt("done", pWriteDialog_TableModel.getRowCount() - 1, 1);
				}
			}
		} catch (Exception ex2) {
			ex2.printStackTrace();
			if (com.pfsbuilder.Global.GUI) {
				new PExceptionDialog(ex2, com.pfsbuilder.Global.sessionNumber).setVisible(true);
			}
		}
		// update tree
		pOKButton.setVisible(false);
		pCancelButton.setVisible(false);
		pWarningLabel.setText("Please wait, updating the tree");
		((PFSTreeModel) jDirectoryTree.getModel()).fireTreeStructureChanged(currentDirectory);
		this.setVisible(false);
	}

	private void writeFileContent(String filename, long size) throws Exception {
		FileWriter fw = new FileWriter(filename);
		char fromChar = pFileContentFromTextField.getText().charAt(0);
		char toChar = pFileContentToTextField.getText().charAt(0);
		char c = fromChar;
		char buffer[] = new char[1024 * 1024];
		// for (int index = 0; index < size; index++) {
		// if (stopThread || this.isVisible() == false) {
		// pOKButton.setText("OK");
		// return;
		// } else if (JRadioButton1.isSelected()) {
		// fw.write(c);
		// fw.flush();
		// c++;
		// if (c == toChar + 1) {
		// c = fromChar;
		// }
		// } else {
		// fw.write(pFileContentTextField.getText().charAt(0));
		// }
		// if (index % (1024 * 1024 - 1) == 0) {
		// pWriteDialog_TableModel.setValueAt((index + 1) / 1024 / 1024 +
		// "MB written",
		// pWriteDialog_TableModel.getRowCount() - 1, 1);
		// }
		// }
		int bufferIndex = 0;
		for (int index = 0; index < size; index++) {
			while (stopThread) {
				Thread.currentThread().sleep(1000);
			}
			if (this.isVisible() == false) {
				return;
			}

			if (JRadioButton1.isSelected()) {
				// fw.write(c);
				// fw.flush();
				buffer[bufferIndex] = c;
				c++;
				if (c == toChar + 1) {
					c = fromChar;
				}
			} else {
				buffer[bufferIndex] = pFileContentTextField.getText().charAt(0);
				// fw.write(pFileContentTextField.getText().charAt(0));
			}
			bufferIndex++;
			if (bufferIndex == buffer.length) {
				fw.write(buffer);
				pWriteDialog_TableModel.setValueAt((index + 1) / 1024 / 1024 + "MB written", pWriteDialog_TableModel.getRowCount() - 1, 1);
				bufferIndex = 0;
			}
		}
		fw.write(buffer, 0, bufferIndex);
		fw.close();
	}

	public void pCancelButton_actionPerformed(ActionEvent e) {
		File currentDirectory = (File) jDirectoryTree.getLastSelectedPathComponent();
		((PFSTreeModel) jDirectoryTree.getModel()).fireTreeStructureChanged(currentDirectory);
		this.setVisible(false);
	}
}

class PAutoGenerateDialog_pCancelButton_actionAdapter implements ActionListener {
	private AutoGenerateDialog adaptee;

	PAutoGenerateDialog_pCancelButton_actionAdapter(AutoGenerateDialog adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent e) {
		adaptee.pCancelButton_actionPerformed(e);
	}
}

class PAutoGenerateDialog_pOKButton_actionAdapter implements ActionListener {
	private AutoGenerateDialog adaptee;

	PAutoGenerateDialog_pOKButton_actionAdapter(AutoGenerateDialog adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent e) {
		adaptee.pOKButton_actionPerformed(e);
	}
}
